package com.zjlp.seproject.controller;

import com.zjlp.seproject.pojo.*;
import com.zjlp.seproject.service.SubjectService;
import com.zjlp.seproject.service.UserService;
import com.zjlp.seproject.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.UUID;

@RestController
public class FileController {
    @Autowired
    private SubjectService subjectService;

    @Autowired
    private UserService userService;

    /**
     *
     * @param file  文件
     * @return  返回文件的存储路径 可直接通过路径访问
     */
    @CrossOrigin
//    @RequestMapping(value="/upload", produces="multipart/form-data;charset=UTF-8")
    @RequestMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) {
        if (file == null) {
            System.out.println("file is null");
            return null;
        }
        String result = "";
        if (file != null) {
            try {
                String filePath = "D:/IDEA/seproject/src/main/resources/static/imgs/";
                String uuid = UUID.randomUUID().toString();
                String fileName = uuid + file.getOriginalFilename();
                System.out.println(filePath);
                System.out.println(fileName);
                File f = new File(filePath + fileName);
                result = "http://localhost:8443/imgs/" + fileName;
                System.out.println(result);
                file.transferTo(f);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     *
     * @param addSubject  添加项目的AddSubject对象
     * @param authorization  请求头token
     * @param response   相应
     * @return  返回是否成功
     */
    @CrossOrigin
    @RequestMapping("/addSubject")
    public Integer addSubject(@RequestBody AddSubject addSubject,
                          @RequestHeader("Authorization") String authorization, HttpServletResponse response) {
        if (!JwtUtil.verify(authorization)) {
            response.setStatus(999);
            return null;
        }
        String idStr = JwtUtil.getUserId(authorization);
        addSubject.setUid(Integer.parseInt(idStr));
        System.out.println("addSubject");
        String filePath = "D:/IDEA/seproject/src/main/resources/static/markdown/";
//        System.out.println(addSubject.getDescription());
        String uuid = UUID.randomUUID().toString();
        String path = filePath + uuid + ".txt";
        File file = new File(path);
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(path));
            bw.write(addSubject.getDescription());
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        addSubject.setDescription(path);
//        System.out.println(path);
        return subjectService.addSubject(addSubject);
    }

    /**
     *
     * @param id  项目id
     * @return   返回SubjectDetails对象
     */
    @CrossOrigin
    @RequestMapping("/getSubject")
    @ResponseBody
    public SubjectDetails getSubject(@RequestBody ID id) {
        System.out.println("getSubject:" + id.getId());
        String markdown = "";
        String thisLine;
        SubjectDetails sd = subjectService.selectByPrimaryKey(id.getId());
        if (sd == null) {
            return null;
        }
        String path = sd.getDescription();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
            while((thisLine = br.readLine()) != null) {
                markdown += thisLine + "\n";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
//        System.out.println(markdown);
        sd.setDescription(markdown);
        subjectService.click(id.getId(), sd.getClick());
        return sd;
    }

//    @CrossOrigin
//    @RequestMapping("/setAnnex")
//    @ResponseBody
//    public void setAnnex(@RequestBody Annex annex) {
//        System.out.println("setAnnex");
//        System.out.println(annex.getId());
//        List<String> list = annex.getList();
//        System.out.println(list.size());
//        for (String s : list) {
//            System.out.println(s);
//        }
////        subjectService.setAnnex(annex);
//    }

    /**
     *
     * @param id  项目的id
     * @return  返回附件的路径lsit
     */
    @CrossOrigin
    @RequestMapping("/getAnnex")
    @ResponseBody
    public List<String> getAnnex(@RequestBody ID id) {
        return subjectService.getAnnex(id.getId());
    }

    /**
     *
     * @param file  传入文件
     * @param authorization   请求头token
     * @return   返回是否成功
     */
    @CrossOrigin
//    @RequestMapping(value="/upload", produces="multipart/form-data;charset=UTF-8")
    @RequestMapping("/avatar")
    public String avatar(@RequestParam("file") MultipartFile file, @RequestHeader("Authorization") String authorization) {
        System.out.println("avatar");
        String result = "";
        if (file != null) {
            try {
                String filePath = "D:/IDEA/seproject/src/main/resources/static/avatar/";
                String uuid = UUID.randomUUID().toString();
                String fileName = uuid + file.getOriginalFilename();
//                System.out.println(filePath);
//                System.out.println(fileName);
                File f = new File(filePath + fileName);
                result = "http://localhost:8443/avatar/" + fileName;
                System.out.println(result);
                file.transferTo(f);
            } catch (IOException e) {
                e.printStackTrace();
            }
            User user = new User();
            user.setId(Integer.parseInt(JwtUtil.getUserId(authorization)));
            user.setAvatar(result);
            userService.avatar(user);
        }
        return result;
    }

    /**
     *
     * @param file  传入附件
     * @return  返回路径
     */
    @CrossOrigin
//    @RequestMapping(value="/upload", produces="multipart/form-data;charset=UTF-8")
    @RequestMapping("/annex")
    public String annex(@RequestParam("file") MultipartFile file) {
        String result = "";
        if (file != null) {
            try {
                String filePath = "D:/IDEA/seproject/src/main/resources/static/annex/";
                String uuid = UUID.randomUUID().toString();
                String fileName = uuid + file.getOriginalFilename();
//                System.out.println(filePath);
//                System.out.println(fileName);
                File f = new File(filePath + fileName);
                result = "http://localhost:8443/annex/" + fileName;
                System.out.println(result);
                file.transferTo(f);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
